﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsGame1.CoreComponents;
using WindowsGame1.SpriteClasses;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame1
{
    class CombatScreen : GameScreen
    {
        PlayerComponent player;
        PlayerCharacter you;
        //I passed the player character in here because the playerCharacter has the HP, MP etc
        //not the player component. There is no sense in tying hp to the player component when it is in the character already
        Monster monster;
        List<LaserBall> myBalls = new List<LaserBall>();
        List<LaserBall> enemyBalls = new List<LaserBall>();

        BackgroundComponent background;
        Vector2 oldPosition;
        AnimationKey oldAnimation;
        int screenHeight;
        int screenWidth;
        bool battleOver;
        bool won;

        public CombatScreen(Game game, PlayerCharacter play)//new parameter for the passing of the character added
            : base((Game1)game)
        {
            screenHeight = Game.Window.ClientBounds.Height;
            screenWidth = Game.Window.ClientBounds.Width;
            you = play;//sets the playerCharacter
            battleOver = false;
            won = true;
        }

        public void Begin(Texture2D image, PlayerComponent player, Monster monster)
        {
            this.player = player;
            this.monster = monster;
            background = new BackgroundComponent(game, image, true);
            background.Enabled = true;
            background.Visible = true;
            childComponents.Add(background);
            oldPosition = player.Position;
            oldAnimation = player.Animation;
            player.Position = new Vector2(
            25,
            (screenHeight - 100 - player.SpriteHeight) / 2);
            monster.Position = new Vector2(
            screenWidth - 25 - monster.Width,
            (screenHeight - 100 - monster.Height) / 2);
            monster.CurrentAnimation = AnimationKey.Left;
            monster.IsAnimating = false;
            monster.InCombat = true;
            player.Animation = AnimationKey.Right;
            player.IsAnimating = false;
            player.InCombat = true;
        }

        public void End()
        {
            childComponents.Remove(background);
            player.InCombat = false;
            player.Position = oldPosition;
            player.Animation = oldAnimation;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            player.Update(gameTime);
            monster.Follow(((int)player.Position.Y - (int)monster.Position.Y));
            monster.Update(gameTime);
            if (0 == monster.Recharge)
                createBall(false);
            //Since you can't modify the containment structure inside a foreach loop
            //I turned this into a for loop and used the removeAt. There is a line of code
            //for decrementing the index of the pointer because when you remove, stuff shifts 
            //and the pointers are no longer what they were before
            for (int i=0; i<myBalls.Count; i++)
            {
                myBalls[i].Update(gameTime);
                if (goodHit(myBalls[i].Position, monster.Position))
                {
                    myBalls.RemoveAt(i);
                    i--;
                    monster.getHit();
                    if (0 == monster.getHP())
                        battleOver = true;
                }
                
            }
            for (int i = 0; i < enemyBalls.Count; i++)
            {
                enemyBalls[i].Update(gameTime);
                if (goodHit(player.Position, enemyBalls[i].Position))
                {
                    enemyBalls.RemoveAt(i);
                    i--;
                    you.HitPointsCurrent -= 5;
                    if (you.HitPointsCurrent == 0)//FAIL
                    {
                        battleOver = true;
                        won = false;
                    }
                }
            }
        }

        public bool BattleOver
        {
            get { return battleOver; }
            set { battleOver = value; }
        }

        public bool Won
        {
            get { return won; }
        }

        /// <summary>
        /// A method which determins if laser hits target.
        /// To use for hitting monster, put laserPos 1st and monsterPos 2nd.
        /// To use for hitting player, put playerPos 1st and laserPos 2nd.
        /// </summary>
        /// <param name="pos1"></param>
        /// <param name="pos2"></param>
        /// <returns></returns>
        public bool goodHit(Vector2 laserPos, Vector2 monsterPos)
        {
            if (10 > (monsterPos.X - laserPos.X) && 0 <= (monsterPos.X - laserPos.X))
            {
                if (30 > (monsterPos.Y - laserPos.Y) && -30 <= (monsterPos.Y - laserPos.Y))
                    return true;
            }
            return false;
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            player.Draw(gameTime);
            monster.Draw(gameTime);
            for (int i = 0; i < myBalls.Count; i++)
            {
                if (myBalls[i].Position.X < 1000)
                    myBalls[i].Draw(gameTime);
                else
                {
                    myBalls.RemoveAt(i);
                    i--;
                }
            }
            for (int i = 0; i < enemyBalls.Count; i++)
            {
                if (enemyBalls[i].Position.X < 1000)
                    enemyBalls[i].Draw(gameTime);
                else
                {
                    enemyBalls.RemoveAt(i);
                    i--;
                }
            }
        }

        public void createBall(bool isMine)
        {
            Vector2 position = new Vector2();
         
            if (isMine)
            {
                LaserBall ball = new LaserBall(
                    this.game,
                    Content.Load<Texture2D>(@"Sprites\ball"), player.Position, isMine);
                position.X = player.Position.X;
                position.Y = player.Position.Y;
                ball.Position = position;
                myBalls.Add(ball);
            }
            else
            {
                LaserBall ball = new LaserBall(
                    this.game,
                    Content.Load<Texture2D>(@"Sprites\ball"), monster.Position, isMine);
                position.X = monster.Position.X;
                position.Y = monster.Position.Y;
                ball.Position = position;
                enemyBalls.Add(ball);
            }
        }

        public Monster Monster
        {
            get { return monster; }
        }
    }
}
